package OwnSideObject;

import PropsObject.*;
import com.sxt.*;
import enemyObject.*;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 我方战斗机
 */
public class PlaneObj extends OwnSideObject{
    public static int MY_PLANE_WIDTH = 102; //我方飞机的宽度
    public static int MY_PLANE_HEIGHT = 126; //我方飞机的高度
    public static int LIFE_WIDTH = 46; //生命值图标的宽度
    public static int LIFE_HEIGHT = 57; //生命值图标的高度
    public static int BOMB_WIDTH = 63; //爆炸道具图标的宽度
    public static int BOMB_HEIGHT = 57; //爆炸道具图标的高度

    //标志
    public int flag;
    //我方飞机爆炸动画效果的图片张数
    public static int AnimationNums = 4;
    //我方飞机发射子弹模式
    public boolean[] BulletMode;
    public boolean[] BulletMode_backup;
    public int modeLast;
    //我方飞机血量
    public int life=200;
    //我方飞机获得的炸弹道具数量

    public int bombNum=0;
    //我方飞机是否损坏
    public static boolean isDestroyed =false;
    //飞机损毁需要的重次数
    int PicCount = 4;
    boolean animation0 =true;

    public PlaneObj() {
        super();
    }

    public PlaneObj(String image, int x, int y, int speedY, GameWin frame,int flag) {
        super(image, x, y, speedY, frame);
        BulletMode = new boolean[3];
        BulletMode[0] = true;
        BulletMode_backup=BulletMode.clone();
        modeLast = Integer.MAX_VALUE;
        this.flag=flag;
    }

    public PlaneObj(String image, GameWin frame) {
        super(image, frame);
    }

    public PlaneObj(String image, int x, int y, int width, int height, int speedY, GameWin frame) {
        super(image, x, y, width, height, speedY, frame);
    }

    @Override
    public void paintSelf(Graphics s) {
        //动画替身
        if (this.flag==2){
            this.Frame.animationObjs.add(new AnimationObj("images/me_destroy_",".png",4,this.x,this.y,this.Frame));
            this.Frame.removeList.add(this);
        }

        //我方飞机显示血量
        //应该显示在窗口的左下角
        Image Life_Image = Toolkit.getDefaultToolkit().getImage("images/life.png");
        s.drawImage(Life_Image,20,GameWin.WINDOW_HEIGHT - LIFE_HEIGHT - 20,null);
        s.setColor(Color.BLACK);
        s.setFont(new Font("微软雅黑",Font.BOLD,20));
        s.drawString("×"+this.life,LIFE_WIDTH + 25,GameWin.WINDOW_HEIGHT - LIFE_HEIGHT + 5);
        //道具计数
        Image bombNum = Toolkit.getDefaultToolkit().getImage("images/bomb.png");
        s.drawImage(bombNum,13,GameWin.WINDOW_HEIGHT - LIFE_HEIGHT - BOMB_HEIGHT - 20,null);
        s.setColor(Color.BLACK);
        s.setFont(new Font("微软雅黑",Font.BOLD,20));
        s.drawString("×"+this.bombNum,BOMB_WIDTH + 23,GameWin.WINDOW_HEIGHT - LIFE_HEIGHT - BOMB_HEIGHT + 10);

        if (modeLast!=Integer.MAX_VALUE&&BulletMode[2]){
            modeLast--;
        }

        if (modeLast<=0&&BulletMode[2]){
            modeLast=Integer.MAX_VALUE;
            BulletMode=BulletMode_backup.clone();
            BulletMode[2] = false;
        }

        if (animation0){
            s.drawImage(Toolkit.getDefaultToolkit().getImage("images/me_"+this.Frame.plane_choice+"1.png"),x,y,null);
            animation0=false;
        }else {
            s.drawImage(Toolkit.getDefaultToolkit().getImage("images/me_"+this.Frame.plane_choice+"2.png"),x,y,null );
            animation0=true;
        }

        //添加鼠标事件，飞机跟随鼠标移动
        this.Frame.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                //让飞机跟随鼠标移动
                x = e.getX()-MY_PLANE_WIDTH / 2;
                y = e.getY()-MY_PLANE_HEIGHT / 2;
            }
        });

        //我方飞机与敌方boss的碰撞检测
        if(this.Frame.bossObj!=null&&this.getRec().intersects(this.Frame.bossObj.getRec())){
            this.x=240;
            this.y=400;
            this.life-=2;
        }

        //我方飞机与敌方子弹的碰撞检测
        if (!this.Frame.enemyBulletObjs.isEmpty())
            for (EnemyBulletObj enemyBulletObj:this.Frame.enemyBulletObjs){
                if (this.getRec().intersects(enemyBulletObj.getRec())){
                    enemyBulletObj.x=GameWin.ENEMY_VANISH_LOCATION_X;
                    enemyBulletObj.y=GameWin.ENEMY_VANISH_LOCATION_Y;
                    this.Frame.removeList.add(enemyBulletObj);
                    this.life--;
                }
            }

        //我方飞机与道具碰撞检测
        //炸弹道具检测
        if (!this.Frame.bombPropsObjs.isEmpty())
            for (BombPropsObj bombPropsObj:this.Frame.bombPropsObjs){
                if (this.getRec().intersects(bombPropsObj.getRec())){
                    GameMusic.get_bomb.play();
                   bombPropsObj.x=GameWin.MY_BULLET_VANISH_LOCATION_X;
                   bombPropsObj.y=GameWin.MY_BULLET_VANISH_LOCATION_Y;
                   this.Frame.removeList.add(bombPropsObj);
                   this.Frame.PropsisExist=false;
                   this.bombNum++;
                }
            }
        //子弹道具检测
        if (!this.Frame.bulletPropsObjs.isEmpty())
            for (BulletPropsObj bulletPropsObj:this.Frame.bulletPropsObjs)
                if (this.getRec().intersects(bulletPropsObj.getRec())){
                    GameMusic.bullet.play();
                    bulletPropsObj.x=GameWin.MY_BULLET_VANISH_LOCATION_X;
                    bulletPropsObj.y=GameWin.MY_BULLET_VANISH_LOCATION_Y;
                    this.Frame.removeList.add(bulletPropsObj);
                    this.Frame.PropsisExist=false;
                   BulletMode_backup=BulletMode.clone();
                   BulletMode[1] = true;
                   BulletMode[2] = true;
                   this.modeLast = PropsObject.PropsLast;
                    System.out.println("子弹道具检测到了");
                }

        //我方飞机与敌方飞机碰撞检测
        //与敌方1号飞机检测
        if (!this.Frame.enemy1ObjList.isEmpty())
            for (EnemyObj enemyObj:this.Frame.enemy1ObjList)
                if (this.getRec().intersects(enemyObj.getRec())){
                    this.Frame.animationObjs.add(new AnimationObj(EnemyObj.AnimationPath,EnemyObj.PicFormat,EnemyObj.AnimationNums,enemyObj.x,enemyObj.y,enemyObj.Frame));
                    enemyObj.x=GameWin.ENEMY_VANISH_LOCATION_X;
                    enemyObj.y=GameWin.ENEMY_VANISH_LOCATION_Y;
                    this.Frame.removeList.add(enemyObj);
                    this.life-=10;
                    this.Frame.bacGroundObj.score+=enemyObj.score;
                }
        //与敌方2号飞机检测
        if (!this.Frame.enemy2ObjList.isEmpty())
            for (Enemy2Obj enemy2Obj:this.Frame.enemy2ObjList)
                if (this.getRec().intersects(enemy2Obj.getRec())){
                    this.Frame.animationObjs.add(new AnimationObj(Enemy2Obj.AnimationPath,Enemy2Obj.PicFormat,Enemy2Obj.AnimationNums,enemy2Obj.x,enemy2Obj.y,enemy2Obj.Frame));
                    enemy2Obj.x=GameWin.ENEMY_VANISH_LOCATION_X;
                    enemy2Obj.y=GameWin.ENEMY_VANISH_LOCATION_Y;
                    this.Frame.removeList.add(enemy2Obj);
                    this.life-=10;
                    this.Frame.bacGroundObj.score+=enemy2Obj.score;
                }

        if (this.life<=0){
            GameMusic.me_down.play();
            this.Frame.animationObjs.add(new AnimationObj("images/me_destroy_",".png",4,this.x,this.y,this.Frame));
            isDestroyed=true;
        }
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x,y,MY_PLANE_WIDTH,MY_PLANE_HEIGHT);
    }

    @Override
    public void DeBug() {
        System.out.println("PlaneObj Class");
    }
}
